<?php

/*
 * Управление новостями
 */

namespace APPS\NEWS\CONTROLLERS\ADMIN;

use APPS\NEWS\MODELS\Rubric;
use APPS\NEWS\MODELS\Article;
use T4\Exception;

class Articles extends \T4\Controller {
	
	protected function preAction() {
		$rubrics = Rubric::findAll();
		if ( count($rubrics) == 0 )
			$this->redirect('/admin/news/Rubrics');
	}

	public function actionDefault() {
		
		$articles = Article::findAll();
		$this->view->assign('articles', $articles);
		$this->view->assign('article', new Article);

		return $this->view->render('admin/articles/index.html');
		
	}
	
	public function actionSave() {

		if (!empty($_REQUEST['id']))
			$article = Article::findByPK(intval($_REQUEST['id']));
		else
			$article = new Article;

		try {

			$article->mergeArray($_POST);

			$article->save();

			$this->redirect('/admin/news/Articles');
			
		} catch (Exception $e) {

			throw $e;
		
		}

		$this->view->assign('article', $article);
		return 
			$this->view->render('admin/articles/article.html');
		
	}
	
	public function actionEdit() {

		$article = Article::findByPK(intval($_REQUEST['id']));
		$this->view->assign('article', $article);

		return 
			$this->view->render('admin/articles/article.html');
		
	}
	
}